package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.FeedBack;
import henu.dao.IFeedBackDao;
import henu.util.Dhcp;

public class FeedBackDaoImpl implements IFeedBackDao {
	protected QueryRunner qr = null;
	
	public FeedBackDaoImpl(QueryRunner qr){ 
		this.qr = qr;
	}
	@Override
	public int save(FeedBack fb) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "insert into tb_feedback values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] param = {fb.getfId(),fb.getfTeacherName(),fb.getfLessonName(),fb.getfSubjectNum(),fb.getfClassOrd(),fb.getfSchedule(),fb.getfAdvice(),fb.getfClassSitutation(),fb.getfAfterClassSitu(),fb.getfClassEfect(),fb.getfWeekTime(),fb.getfHandlingSuggestion(),fb.getfMemo()};
		int n = 0;
		try {
			n = qr.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int delete(String fId) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "delete from tb_feedback where fId = ?";
		int n = 0;
		try {
			n = qr.update(sql, fId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int update(String fId, FeedBack fb) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "update tb_feedback set fSubjectNum=?,fClassOrd=?,fQuesType=?,fQuesContent=?,fClassSitutation=?,fAfterClassSitu=?,fClassEfect=?,fWeekTime=?,fHandlingSuggestion=?,fMemo=? where fId = ?";
		Object[] param = {fb.getfId(),fb.getfTeacherName(),fb.getfLessonName(),fb.getfSubjectNum(),fb.getfClassOrd(),fb.getfSchedule(),fb.getfAdvice(),fb.getfClassSitutation(),fb.getfAfterClassSitu(),fb.getfClassEfect(),fb.getfWeekTime(),fb.getfHandlingSuggestion(),fb.getfMemo()};
		int n = 0;
		try {
			n = qr.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public List<FeedBack> select(String province,String value) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql1 = "select fTeacherName,fLessonName,fClassEfect,fSchedule,fClassOrd,fClassSitutation,fAfterClassSitu,fAdvice from tb_feedback where fTeacherName=?";
		String sql2 = "select fWeekTime,fLessonName,fTeacherName,fClassEfect,fSchedule,fClassOrd,fClassSitutation,fAfterClassSitu,fAdvice from tb_feedback where fWeekTime=?";
		String sql3 = "select fLessonName,fClassEfect,fSchedule,fClassOrd,fClassSitutation,fAfterClassSitu,fAdvice from tb_feedback where fLessonName=?";
		Object param[]={value};
		List<FeedBack> fb = null;
		System.out.println("分析");
		if(province.equals("teacher"))
		{
			try {
				fb= qr.query(sql1,param,new BeanListHandler<FeedBack>(FeedBack.class));
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		if(province.equals("week"))
		{
			try {
				fb= qr.query(sql2,param,new BeanListHandler<FeedBack>(FeedBack.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		if(province.equals("course"))
		{
			try {
				fb= qr.query(sql3,param,new BeanListHandler<FeedBack>(FeedBack.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return fb;
	}
	@Override
	public List<FeedBack> getAll() {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_feedback";
		List<FeedBack> fbs = null;
		try {
			fbs = qr.query(sql, new BeanListHandler<FeedBack>(FeedBack.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return fbs;
	}	
	@Override
	public List<FeedBack> getAll(int pageSize, int pageNow) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_feedback order by fId desc limit "+(pageNow*pageSize-pageSize)+","+pageSize;
		List<FeedBack> fbs = null;
		try {
			fbs = qr.query(sql, new BeanListHandler<FeedBack>(FeedBack.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return fbs;
	}
	@Override
	public List<FeedBack> getByWeek(String weektime) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_feedback where fWeekTime=?";
		Object param[]={weektime};
		List<FeedBack> fbs = null;		
		try {
			fbs = qr.query(sql,param, new BeanListHandler<FeedBack>(FeedBack.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fbs;
	}
	@Override
	public List<FeedBack> getByName(String teachername) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_feedback where fTeacherName=?";
		Object param[]={teachername};
		List<FeedBack> fbs = null;		
		try {
			fbs = qr.query(sql,param, new BeanListHandler<FeedBack>(FeedBack.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fbs;
	}
	@Override
	public List<FeedBack> getByLesson(String lesson) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_feedback where fLessonName=?";
		Object param[]={lesson};
		List<FeedBack> fbs = null;		
		try {
			fbs = qr.query(sql,param, new BeanListHandler<FeedBack>(FeedBack.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fbs;
	}
	@Override
	public int getCount() {
		String sql = "select * from tb_feedback";
		List<FeedBack> fb = null;
		try {
			fb = qr.query(sql, new BeanListHandler<FeedBack>(FeedBack.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fb.size();
	}
	
}
